package com.cskaoyan.aspectj;

import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.MarketLog;
import com.cskaoyan.service.LogService;
import com.cskaoyan.service.OrderService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

import static com.cskaoyan.utils.HttpUtil.addLog;

/**
 * @Author: JianhuaZeng
 * @Date: 2022/12/31 16:11
 */
@Component
@Aspect
public class OrderLogAspect {

    @Autowired
    LogService logService;

    @Autowired
    OrderService orderService;

    private String getAdmin() {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return "";
        }
        MarketAdmin principal = (MarketAdmin) subject.getPrincipal();
        return principal.getUsername();
    }

    @Pointcut("@annotation(com.cskaoyan.aspectj.order.OrderDelete)")
    public void pointcutOrderDelete() {
    }

    @Pointcut("@annotation(com.cskaoyan.aspectj.order.OrderShip)")
    public void pointcutOrderShip() {
    }

    @Pointcut("@annotation(com.cskaoyan.aspectj.order.OrderRefund)")
    public void pointcutOrderRefund() {
    }

    /**
     * @description: 删除订单
     * @param: joinPoint
     * @return: java.lang.Object
     * @author: JianhuaZeng
     * @date: 2023/1/2 2023/1/2
     */
    @Around("pointcutOrderDelete()")
    public Object orderDeleteLog(ProceedingJoinPoint joinPoint) throws Throwable {
        Object res = joinPoint.proceed();
        Map arg = (Map) joinPoint.getArgs()[0];
        Integer orderId = (Integer) arg.get("orderId");
        String orderSn = orderService.queryOrderById(orderId).getOrderSn();
        MarketLog log = addLog(res, getAdmin(), "删除", "订单编号 " + orderSn);
        logService.insert(log);
        return res;
    }

    /**
     * @description: 发货
     * @param: joinPoint
     * @return: java.lang.Object
     * @author: JianhuaZeng
     * @date: 2023/1/2 2023/1/2
     */
    @Around("pointcutOrderShip()")
    public Object orderShipLog(ProceedingJoinPoint joinPoint) throws Throwable {
        Object res = joinPoint.proceed();
        Map arg = (Map) joinPoint.getArgs()[0];
        Integer orderId = (Integer) arg.get("orderId");
        String orderSn = orderService.queryOrderById(orderId).getOrderSn();
        MarketLog log = addLog(res, getAdmin(), "发货", "订单编号 " + orderSn);
        logService.insert(log);
        return res;
    }

    /**
     * @description: 退款
     * @param: joinPoint
     * @return: java.lang.Object
     * @author: JianhuaZeng
     * @date: 2023/1/2 2023/1/2
     */
    @Around("pointcutOrderRefund()")
    public Object orderRefundLog(ProceedingJoinPoint joinPoint) throws Throwable {
        Object res = joinPoint.proceed();
        Map arg = (Map) joinPoint.getArgs()[0];
        Integer orderId = (Integer) arg.get("orderId");
        String orderSn = orderService.queryOrderById(orderId).getOrderSn();
        MarketLog log = addLog(res, getAdmin(), "退款", "订单编号 " + orderSn);
        logService.insert(log);
        return res;
    }
}
